/**
 * @file first_task.c
 * @author lcz (you@domain.com)
 * @brief 
 * @version 0.1
 * @date 2022-10-29
 * 
 * @copyright Copyright (c) 2022
 * 
 */

#include "core/task.h"
#include "tools/log.h"
#include "applib/lib_syscall.h"
#include "dev/tty.h"

int first_task_main(void){


#if 0
    int count = 3;

    int pid = getpid();
    print_msg("first task id=%d", pid);

    pid = fork();
    if (pid < 0)
    {
        print_msg("create child proc failed.", 0);
    }
    else if (pid == 0)
    {
        print_msg("child: %d", count);

        char *argv[] = {"arg0", "arg1", "arg2", "arg3"};
        execve("/shell.elf", argv, (char **)0);
    }
    else
    {
        print_msg("child task id=%d", pid);
        print_msg("parent: %d", count);
    }

     pid=getpid();
#endif
    for (int i = 0; i < TTY_NR; i++)
    {
        int pid = fork();
        if (pid < 0)
        {
            print_msg("create shell failed.", 0);
            break;
        }
        else if (pid == 0)
        {
            char tty_num[] = "/dev/tty?";
            tty_num[sizeof(tty_num) - 2] = i + '0';
            char *argv[] = {tty_num, (char *)0};
            execve("my_shell.elf", argv, (char **)0);
            // 理论上不会出来了，执行shell文件就没有出来了
            while (1)
            {
                msleep(5000);
            }
        }
    }

    for (;;)
    {
        // 系统调用
        // log_printf("first task.");
        // sys_sleep(1000);
        // print_msg("task id=%x", pid);
        // msleep(10000);
        int status;
        wait(&status);
    }
    return 0;
}
